package com.gitee.ricesugar.book.module;

import cn.hutool.core.util.IdUtil;
import com.gitee.ricesugar.entity.Book;
import com.gitee.ricesugar.exception.ErrorException;
import com.gitee.ricesugar.mapper.BookMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;

public class AddBook extends JPanel {
    private BookMapper bookMapper;
    private SqlSession sqlSession;

    public AddBook() throws IOException {
        setLayout(new GridBagLayout());

        createSqlSession();

        // 创建 GridBagConstraints 对象
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);  // 设置组件之间的间距

        // 添加标题
        JLabel titleLabel = new JLabel("添加图书");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 30));
        gbc.gridx = 0;
        gbc.gridy = 0;
        add(titleLabel, gbc);

        // 添加标签和输入框

        // id
        gbc.gridx = 0;
        gbc.gridy = 2;
        add(CreateLabel("图书ID:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 2;
        JTextField idField = CreateTextField();
        add(idField, gbc);

        // title
        gbc.gridx = 0;
        gbc.gridy = 3;
        add(CreateLabel("标题:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 3;
        JTextField titleField = CreateTextField();
        add(titleField, gbc);

        // author
        gbc.gridx = 0;
        gbc.gridy = 4;
        add(CreateLabel("作者:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 4;
        JTextField authorField = CreateTextField();
        add(authorField, gbc);

        // press
        gbc.gridx = 0;
        gbc.gridy = 5;
        add(CreateLabel("出版社:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 5;
        JTextField pressField = CreateTextField();
        add(pressField, gbc);

        // isbn
        gbc.gridx = 0;
        gbc.gridy = 6;
        add(CreateLabel("ISBN:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 6;
        JTextField isbnField = CreateTextField();
        add(isbnField, gbc);

        // category
        gbc.gridx = 0;
        gbc.gridy = 7;
        add(CreateLabel("分类:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 7;
        JTextField categoryField = CreateTextField();
        add(categoryField, gbc);

        // description
        gbc.gridx = 0;
        gbc.gridy = 8;
        add(CreateLabel("描述:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 8;
        JTextField descriptionField = CreateTextField();
        add(descriptionField, gbc);

        // amount
        gbc.gridx = 0;
        gbc.gridy = 9;
        add(CreateLabel("数量:"), gbc);
        gbc.gridx = 1;
        gbc.gridy = 9;
        JTextField amountField = CreateTextField();
        add(amountField, gbc);

        // 添加按钮
        gbc.gridx = 1;
        gbc.gridy = 11;
        JButton addButton = CreateButton("添加");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Book book = new Book();
                try {
                    book.setTitle(titleField.getText());
                    book.setAuthor(authorField.getText());
                    book.setPress(pressField.getText());
                    book.setIsbn(isbnField.getText());
                    book.setCategory(categoryField.getText());
                    book.setDescription(descriptionField.getText());
                    book.setAmount(Integer.parseInt(amountField.getText().equals("") ? "0" : amountField.getText()));
                } catch (NumberFormatException ex) {
                    throw new ErrorException("数量必须为整数");
                }
                try {
                    bookMapper.add(book);
                    sqlSession.commit();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        });
        add(addButton, gbc);

        // 清空按钮
        gbc.gridx = 2;
        gbc.gridy = 11;
        JButton clearButton = CreateButton("清空");
        clearButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                idField.setText("");
                titleField.setText("");
                authorField.setText("");
                pressField.setText("");
                isbnField.setText("");
                categoryField.setText("");
                descriptionField.setText("");
                amountField.setText("");
            }
        });
        add(clearButton, gbc);
    }

    private JLabel CreateLabel(String name){
        JLabel jLabel = new JLabel(name);
        jLabel.setFont(new Font("微软雅黑", Font.BOLD, 15));
        return jLabel;
    }

    private JTextField CreateTextField(){
        JTextField jTextField = new JTextField(20);
        jTextField.setFont(new Font("微软雅黑", Font.BOLD, 15));
        return jTextField;
    }

    private JButton CreateButton(String name){
        JButton jButton = new JButton(name);
        jButton.setFont(new Font("微软雅黑", Font.BOLD, 20));
        return jButton;
    }

    private void createSqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = sqlSessionFactory.openSession();
        bookMapper = sqlSession.getMapper(BookMapper.class);
    }
}
